/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.comm;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamableInNone implements StreamableIn {
	//private BufferedInputStream buffer = null;
	private InputStream instream = null;
	private StreamableIn streamin = null;

	/**
	 * Filter to look for a string as the end of stream marker
	 */
	public StreamableInNone( ) {}
	
	public StreamableInNone( InputStream _instream ) 
	throws IOException {
		setInStream(_instream);
	}
	
	public StreamableInNone( StreamableIn _streamin ) 
	throws IOException {
		setInStream(_streamin);
	}
	
	public void setInStream( StreamableIn _streamin )
	throws IOException {
		streamin = _streamin;
	}

	public void setInStream( InputStream _instream )
	throws IOException {
		if (streamin == null) instream =_instream;
		else streamin.setInStream(_instream);
	}

	public byte[] readBuffer()
	throws IOException {
		if (instream != null) {
			if (instream.available() == 0) return null;
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			for (;instream.available()>0;stream.write(instream.read()));
			/*
			byte buffer [] = new byte[1024];
			String str = "";
			while (instream.available() > 0) {
				int r = instream.read(buffer);
				str += new String(buffer,0,r);
			}
			Log.dev.out("STREAM INPUT NONE >> ",str.getBytes());
			return str;
			*/
			return stream.toByteArray();
		} else
			return streamin.readBuffer();
	}

	// This method is called some buffer needs to be purged
	public void refresh() {}
	
	public void purge() {}
	
	public StreamableIn getNewInstance()  {
		return new StreamableInNone();
	}

}
